package com.kmist.xiakexing.view;


import android.content.Context;
import android.os.Build;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kmist.xiakexing.R;
import com.kmist.xiakexing.adapter.CommonAdapter;
import com.kmist.xiakexing.adapter.ViewHolder;
import com.kmist.xiakexing.interfaces.ITaskCallback;
import com.kmist.xiakexing.presenters.NpcPresenter;
import com.kmist.xiakexing.presenters.PlayerPresenter;
import com.kmist.xiakexing.presenters.TaskPresenter;
import com.kmist.xiakexing.tools.Config;
import com.kmist.xiakexing.tools.DialogRe;
import com.kmist.xiakexing.tools.OnMultiClickListener;
import com.kmist.xiakexing.utils.cmds.Look;
import com.kmist.xiakexing.utils.scene.NpcBean;
import com.kmist.xiakexing.utils.scene.PlayerBean;
import com.kmist.xiakexing.utils.scene.PlayerTaskBean;
import com.kmist.xiakexing.utils.scene.TaskBean;

import java.util.ArrayList;
import java.util.List;

import static com.kmist.xiakexing.utils.Color.STR;

public class NpcMoreInfoDialog extends DialogRe implements View.OnClickListener, ITaskCallback {

    private TextView item_info;
    private TextView item_title;
    private Button item_ask;
    private TextView item_name;
    private Context context;

    private RecyclerView taskView;
    private List<TaskBean> curNpcTaskList = new ArrayList<>();
    private CommonAdapter<TaskBean> taskAdapter;

    private NpcPresenter npcPresenter;
    private PlayerPresenter playerPresenter;
    private TaskPresenter taskPresenter;
    private Look look = Look.getsInstance();
    private boolean task_loaded_tag = false;
    private NpcBean curNpc;
    private PlayerBean me;

    public NpcMoreInfoDialog(Context context) {
        super(context, R.layout.a_login_create_new_player);
    }

    public NpcMoreInfoDialog(Context context, NpcBean npc) {
        super(context, R.layout.obj_action_dialog);
        this.context = context;
        npcPresenter = NpcPresenter.getsInstance();
        curNpc = npcPresenter.getNpcd();
        playerPresenter = PlayerPresenter.getInstance();
        me = playerPresenter.getPlayerd();
        taskPresenter = TaskPresenter.getInstance();
        taskPresenter.registerViewCallback(this);


        Init();
        item_title = mWindow.findViewById(R.id.obj_dialog_title);
        item_name = mWindow.findViewById(R.id.obj_dialog_name);
        item_info = mWindow.findViewById(R.id.obj_dialog_info);
        item_ask = mWindow.findViewById(R.id.obj_dialog_ask);
        taskView = mWindow.findViewById(R.id.task_list);

        showNpcInfo(npc);
        //item_greet.setText(npcPresenter.getGreeting(npc, playerPresenter.getPlayerd()));
        updateTaskList(npc.getNpcTaskList());
        showNpcTask(true, false);
        item_ask.setOnClickListener(this);
    }

    private void showNpcInfo(NpcBean npc) {
        item_title.setText(npc.getTitle());
        item_name.setText(npc.getName());
        item_info.setText(npc.getDesc());

        item_info.append(look.look_info_age(me, npc));
        item_info.append(STR(look.look_info_description(me, npc)));
        item_info.append(STR(look.look_info_tattoo(me, npc)));
        item_info.append(STR(look.look_info_damage(me, npc)));
        item_info.append(STR(look.look_info_releation(me, npc)));
        item_info.append(STR(look.look_info_family(me, npc)));
        item_info.append(STR(look.look_info_equiped(me, npc)));

    }

    private void updateTaskList(List<TaskBean> npcTaskList) {
        for(TaskBean task : npcTaskList){
            String sid = playerPresenter.getPlayerd().getSid();
            PlayerTaskBean pt = taskPresenter.getPlayerTask(sid, task.getTaskId());
            if(pt == null){
                curNpcTaskList.add(task);
            }else{
                //TODO
                if(pt.getTaskStatus() != 3){ //已经完成的任务不显示
                    curNpcTaskList.add(task);

                }else{
                    //item_info.append(taskPresenter.getTaskDialogue(pt.getTaskStatus(), task.getTaskInfo()));
                }
            }
        }
    }

    private void showNpcTask(boolean isVertical, boolean isRevers) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(Config.context);
        layoutManager.setOrientation(isVertical? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
        layoutManager.setReverseLayout(isRevers);

        taskView.setLayoutManager(layoutManager);
        taskAdapter = new CommonAdapter<TaskBean>(Config.context, R.layout.npc_task_info_view, curNpcTaskList) {
            @Override
            protected void convert(ViewHolder holder, TaskBean taskBean) {

                holder.setText(R.id.npc_task_info, taskBean.getTaskName());
                holder.appendText(R.id.npc_task_info, taskPresenter.getTaskStatusStr(taskBean, npcPresenter.getNpcd().getId()));

                holder.setOnClickListener(R.id.npc_task_info, new OnMultiClickListener() {
                    @Override
                    public void onMultiClick(View v) {
                        close();
                        taskPresenter.getTask(taskBean);

                        new TaskInfoDialog(getContext(), taskBean);
                    }
                });

            }
        };
        taskView.setAdapter(taskAdapter);
    }

    public void sureEvent(){
        super.sureEvent();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.obj_dialog_ask:

                break;
        }
    }

    @Override
    public void onTaskLoaded(TaskBean task) {
        task_loaded_tag = true;
    }

    @Override
    public void onTaskLoaded(List<TaskBean> tasks) {
        //NOTODO
    }

    @Override
    public void onTaskLoadEmpty() {
        //NOTODO
    }

    @Override
    public void onTaskLoadError() {
        //NOTODO
    }
}
